package org.filecollector.msgcontroller.answers.responsecodes;

import lombok.NonNull;
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Collection;
import java.util.Hashtable;

public abstract class CodeConvertDictionary {
    private static final Pair<ResponseCode, Integer>[] pairs = new Pair[]{
            Pair.of(ResponseCode.OK, 200),
            Pair.of(ResponseCode.MULTIPLE_CHOICES, 300),
            Pair.of(ResponseCode.BAD_REQUEST, 400),
            Pair.of(ResponseCode.NOT_FOUND, 404),
            Pair.of(ResponseCode.INTERNAL_SERVER_ERROR, 500)
    };

    @NonNull private static final BidiMap<ResponseCode, Integer> dictionary = getBidiMap();

    private static BidiMap<ResponseCode, Integer> getBidiMap() {
        var ret = new DualHashBidiMap<ResponseCode, Integer>();
        for (var pair: pairs) {
            ret.put(pair.getKey(), pair.getValue());
        }
        return ret;
    }
    
    public static Integer get(ResponseCode code) {
        return dictionary.getOrDefault(code, 500);
    }

    public static ResponseCode get(Integer code) {
        return dictionary.inverseBidiMap().getOrDefault(code, ResponseCode.INTERNAL_SERVER_ERROR);
    }
}
